package com.rachel.studyapp.view;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

/**
 * Created by zhengshaorui on 2017/8/12.
 */

public class ToolBarX {
    private static final String TAG = "zsr";

    private Toolbar mToolbar;
    private AppCompatActivity mActivity;
    private ActionBar mActionBar;
    private RelativeLayout mCustomViewLy;

    public ToolBarX(Toolbar toolbar, AppCompatActivity activity) {
        this.mToolbar = toolbar;
        mActivity = activity;
        mActivity.setSupportActionBar(mToolbar);
        mActionBar = mActivity.getSupportActionBar();
    }

    public ToolBarX setTitle(String title) {
        if (mActionBar != null) {
            mActionBar.setTitle(title);
        }
        return this;
    }

    public ToolBarX setSubTitle(String title) {
        if (mActionBar != null) {
            mActionBar.setSubtitle(title);
        }
        return this;
    }

    public ToolBarX setNavigationIcon(int resId) {
        if (mToolbar != null) {
            mToolbar.setNavigationIcon(resId);
        }
        return this;
    }

    public ToolBarX setNavigationOnClickListener(View.OnClickListener listener) {
        if (mToolbar != null) {
            mToolbar.setNavigationOnClickListener(listener);

        }
        return this;
    }

    public ToolBarX setDisplayHomeAsUpEnabled(boolean show) {
        if (mActionBar != null) {
            mActionBar.setDisplayHomeAsUpEnabled(show);
        }
        return this;
    }


    public ToolBarX setCustomView(int viewId) {
        if (mCustomViewLy != null) {
            mCustomViewLy.removeAllViews();
            View view = LayoutInflater.from(mActivity).inflate(viewId, null);
            mCustomViewLy.addView(view);
        }
        return this;
    }

    public ToolBarX removeCustomView(){
        if (mCustomViewLy != null){
            mCustomViewLy.removeAllViews();
        }
        return this;
    }


}
